/**
 * User: 夏汛
 * Date: 12-5-16
 * Time: 上午11:03
 */
Ext.extend(app.module,{
    currentFormValues: {},
    rankRecord:Ext.data.Record.create([
        {name:'id',type:'string'},
        {name:'name',type:'string'},
        {name:'deptname',type:'string'},
        {name:'titlename',type:'string'}
    ]),
    init: function(){
        mainPanel = this;
        this.ktcxbyuserDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'project/listKtcxbyuser.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'description'},
                {name:'projecttype'},
                {name:'state'},
                {name:'reporttime'},
                {name:'approvaltime'},
                {name:'planendtime'},
                {name:'endtime'},
                {name:'tasktime'},
                {name:'middletime'},
                {name:'finishtime'},
                {name:'filepath'},
                {name:'taskpath'},
                {name:'remain'},
                {name:'filename'},
                {name:'projectTypeName'},
                {name:'iscross'},
                {name:'approvedept'},
                {name:'rank'},
                {name:'shoolmoney'},
                {name:'remarks'},
                {name:'deptname'}
            ])
        });

        var ktcxbyuser_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 20, '20条/页' ],[ 50, '50条/页' ], [ 100, '100条/页' ],[ 200, '200条/页' ]]
                }),
            valueField : 'value',
            displayField : 'text',
            value : '20',
            editable : false,
            width : 85
        });
        var number = parseInt(ktcxbyuser_pagesize_combo.getValue());

        ktcxbyuser_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('ktcxbyuserbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            mainPanel.ktcxbyuserDs.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });

        this.loader = new Ext.tree.JsonPluginTreeLoader({dataUrl:'dept/getDeptTree.do'});
        this.rankDs = new Ext.data.Store({
            proxy: new Ext.data.MemoryProxy({'total':1,'rows':[]}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'deptname'},
                {name:'titlename'}
            ])
        });

        this.fileDs = new Ext.data.Store({
            proxy: new Ext.data.MemoryProxy({'total':1,'rows':[]}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'filename'}
            ])
        });

        var userStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'user/listUserByDeptid.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'username'}
            ])
        });
        var comboUser = new Ext.form.ComboBox({
            id:'ktcxbyuserUserid',
            store: userStore,
            editable: false,
            emptyText: '再选择人员...',
            mode: 'local',
            hiddenName : 'userid',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'username',
            valueField: 'id',
            fieldLabel: '人员',
            width:120,
            listeners:{
                "select":function(combo,record,number){
                    this.query();
                },
                scope:this
            }
        });

        var selectDeptTree = new Ext.tree.TreePanel( {
            loader: this.loader,
            root : {
                id: 0,
                text: '根节点',
                expanded: true
            },
            autoScroll : false,
            rootVisible : false,// 根节点是否可见
            animate : false,    // 展开,收缩动画
            useArrows : false,  // 是否使用箭头样式
            lines:false,        // 去掉树的线
            border : false
        });
        // 监听下拉树的节点单击事件
        selectDeptTree.on('click', function(node) {
            if(node.leaf){
                mainPanel.comboxWithDeptTree.setValue(node.text);
                userStore.load({params:{deptid:node.id}});
                mainPanel.comboxWithDeptTree.collapse();
            } else {
                Ext.Msg.alert("提示","不能选择有下级部门的节点！");
            }
        });
        this.comboxWithDeptTree = new Ext.form.ComboBox({
            store : new Ext.data.SimpleStore( {
                fields : [],
                data : [[]]
            }),
            autoScroll : true,
            value : '',
            emptyText : '请先选择所在部门...',
            mode : 'local',
            editable: false,
            triggerAction : 'all',
            maxHeight : 390,
            width:200,
            // 下拉框的显示模板,deptTreeDiv作为显示下拉树的容器
            tpl : "<tpl for='.'><div style='height:390px'><div id='ktcxbyuserSelectDeptTreeDiv'></div></div></tpl>",
            onSelect : Ext.emptyFn,
            onViewClick : Ext.emptyFn,
            assertValue : Ext.emptyFn
        });
        // 监听下拉框的下拉展开事件
        this.comboxWithDeptTree.on('expand', function() {
            // 将树挂到treeDiv容器
            selectDeptTree.render('ktcxbyuserSelectDeptTreeDiv');
        });

        var projectTypeForKtcxbyuserStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'projecttype/list.do'}),
            reader: new Ext.data.JsonReader({
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'iscross'},
                {name:'approvedept'}
            ])
        });
        var comboProjectTypeForktcxbyuser = new Ext.form.ComboBox({
            store: projectTypeForKtcxbyuserStore,
            editable: false,
            emptyText: '项目类型...',
            mode: 'remote',
            hiddenName : 'projectTypeId',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            width:100,
            id:'ktcxbyuserQueryProjectType'
        });

        var comboIsCross = new Ext.form.ComboBox({
            store : new Ext.data.SimpleStore({
                fields : [ 'id', 'name' ],
                data : [[ 0, '纵向课题' ], [ 1, '横向课题' ]]
            }),
            id:'ktcxbyuserQueryIsCross',
            editable: false,
            emptyText: '横向/纵向...',
            mode: 'local',
            hiddenName : 'iscross',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            width:100,
            listeners:{
                "select":function(combo,record,number){
                    if(combo.getValue()==1){
                        projectTypeForKtcxbyuserStore.clearData();
                        comboProjectTypeForKtcxbyuser.reset();
                    } else {
                        projectTypeForKtcxbyuserStore.reload({
                            params : {
                                iscross : combo.getValue()
                            }
                        });
                    }
                },
                scope:this
            }
        });

        this.body = new Ext.grid.GridPanel({
            store: this.ktcxbyuserDs,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            columns: [
                new Ext.grid.RowNumberer(),
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '点击课题名称查看',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        return '<a href="javascript:void(0)" onclick="mainPanel.getFileViewDlg('+
                            record.data["id"]+')">'+value+'</a>';
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'remarks',
                    header: '课题编号'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'approvedept',
                    header: '来源部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'projectTypeName',
                    header: '课题类型'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'approvaltime',
                    width: 50,
                    header: '立项时间',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if (!(value==null || value=='')){
                            return Ext.util.Format.date(new Date(parseInt(value)), 'Y-m')
                        }
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'endtime',
                    width: 50,
                    header: '结题时间',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if (!(value==null || value=='')){
                            return Ext.util.Format.date(new Date(parseInt(value)), 'Y-m')
                        }
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'rank',
                    width: 30,
                    header: '排名'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'state',
                    header: '状态',
                    width: 50,
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if (value>2 && value<11){
                            return "在研";
                        } else if (value==11 ){
                            return "已结题";
                        } else {
                            return "";
                        }
                    }
                }
            ],
            tbar: {
                xtype: 'toolbar',
                items: [
                    {
                        xtype: 'displayfield',
                        value: '选择用户：'
                    },
                    this.comboxWithDeptTree,
                    '-',
                    comboUser,
                    '->',
                    {
                        xtype: 'displayfield',
                        value: '其它查询条件：'
                    },
                    {
                        xtype: 'datefield',
                        name: 'startdt',
                        id: 'ktcxbyuserstartdt',
                        vtype: 'daterange',
                        endDateField: 'ktcxbyuserenddt',
                        emptyText: '立项时间(开始)',
                        width: 110,
                        format:'Y-m-d'
                    },
                    '-',
                    {
                        xtype: 'datefield',
                        name: 'enddt',
                        id: 'ktcxbyuserenddt',
                        vtype: 'daterange',
                        startDateField: 'ktcxbyuserstartdt',
                        emptyText: '立项时间(结束)',
                        width: 110,
                        format:'Y-m-d'
                    },
                    {
                        text : '查询',
                        iconCls : 'btn-search',
                        handler : function() {
                            mainPanel.query();
                        }
                    }

                ]
            },
            bbar: {
                id: 'ktcxbyuserbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.ktcxbyuserDs,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', ktcxbyuser_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
    },
    query: function(){
        var userid = Ext.getCmp('ktcxbyuserUserid').getValue();
        if(userid==''){
            Ext.Msg.alert('提示','还未选择人员');
        } else {
            this.ktcxbyuserDs.load( {
                params : {
                    start : 0,
                    limit : Ext.getCmp('ktcxbyuserbbar').pageSize,
                    userid : Ext.getCmp('ktcxbyuserUserid').getValue(),
                    startTime : dateFormat(Ext.getCmp('ktcxbyuserstartdt').getValue()),
                    endTime : dateFormat(Ext.getCmp('ktcxbyuserenddt').getValue())
                }
            });
        }
    }
});